package com.ub.party.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.ub.party.common.constants.RespCodeEnum;
import com.ub.party.common.vo.resp.RespVo;
import com.ub.party.internal.service.ShreecService;
import com.ub.party.internal.utils.Md5Utils;
import com.ub.party.web.base.CookieHelper;
import com.ub.party.web.constants.ShreecConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.UUID;

@Controller
public class ActivityMgtController extends BaseController {

  private static final Logger logger = LoggerFactory.getLogger(ActivityMgtController.class);
  private static final String ACTIVE_TAG = "tag";

  @Autowired
  private ShreecService shreecService;

  @Autowired
  private CookieHelper cookieHelper;

  /**
   * 活动管理
   *
   * @param model
   * @param request
   * @return
   */
  @RequestMapping(value = {"/MyActivity/index", "/MyActivity/myActivity"}, method = RequestMethod.GET)
  public String myActivity(Model model, HttpServletRequest request) {
//        model.addAttribute("homePath", applicationPropertyConfig.getHomePath());
    return "mgt/activityMgt";
  }

  /**
   * 票券审核
   *
   * @param model
   * @param request
   * @return
   */
  @RequestMapping(value = "/MyActivity/myTicketList", method = RequestMethod.GET)
  public String myTicketList(Model model, HttpServletRequest request) {
//        model.addAttribute("homePath", applicationPropertyConfig.getHomePath());
    return "mgt/myTicket";
  }

  /**
   * 票券审核
   *
   * @param model
   * @param request
   * @return
   */
  @RequestMapping(value = "/MyActivity/ticketChecking", method = RequestMethod.GET)
  public String ticketChecking(Model model, HttpServletRequest request) {
//        model.addAttribute("homePath", applicationPropertyConfig.getHomePath());
    return "mgt/ticketChecking";
  }

  /**
   * 收入提现
   *
   * @param model
   * @param request
   * @return
   */
  @RequestMapping(value = "/MyActivity/income", method = RequestMethod.GET)
  public String income(Model model, HttpServletRequest request) {
//        model.addAttribute("homePath", applicationPropertyConfig.getHomePath());
    return "mgt/income";
  }

  @ResponseBody
  @RequestMapping(value = "myActivity/getMyActivityList", method = RequestMethod.GET)
  public RespVo getMyActivityList(HttpServletRequest request, @RequestParam("type") int type, @RequestParam("pageId") int pageId) {
    return RespVo.ok();
  }

  @ResponseBody
  @RequestMapping(value = "login", method = RequestMethod.POST)
  public Object login(HttpServletRequest request, HttpServletResponse response, @RequestParam("username") String username, @RequestParam("password") String password) {
    String encrypt = Md5Utils.encrypt(password);
    logger.info(String.format("login info, username[%s], password[%s], encrypt[%s].", username, password, encrypt));
    RespVo respVo = shreecService.tryLogin(username, encrypt);
    JSONObject jsonObject = new JSONObject();
    if (RespCodeEnum.SUCCESS.getCode() == respVo.getCode()) {
      jsonObject.put("success", true);

      String uuid = UUID.randomUUID().toString();
      cookieHelper.setCookie(response, ShreecConstants.SHREEC_SESSION_KEY, uuid);
      HttpSession session = request.getSession(true);
      session.setAttribute(ShreecConstants.SHREEC_SESSION_KEY, uuid);
    } else {
      jsonObject.put("success", false);
    }
    return jsonObject;
  }
}


